//
//  UIViewController+NavigationAdition.m
//  XFilm
//
//  Created by TuanNT on 5/15/14.
//  Copyright (c) 2014 NetViet. All rights reserved.
//

#import "UIViewController+NavigationAdition.h"

static char kVCLeftButtonActionHandleTouchUpInsideBlockKey;
static char kVCActionHandleLeftButtonKey;

@implementation UIViewController (NavigationAdition)

- (void)setNavigationLeftItem:(UIImage *)image
                    withBlock:(HandleBlock)block {
	[self setNavigationLeftItem:image andTitle:nil withBlock:block];
}

- (void)setNavigationLeftItem:(UIImage *)image
                     andTitle:(NSString *)title
                    withBlock:(HandleBlock)block {
	UIButton *associateButton = objc_getAssociatedObject(self, &kVCActionHandleLeftButtonKey);

	if (associateButton == nil) {
		UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

		if (image) {
			button.frame = CGRectMake(0, 0, image.size.width, image.size.height);
			[button setBackgroundImage:image forState:UIControlStateNormal];
		}
		else {
			CGSize titeSize = CGSizeZero;
			if ([UIDevice currentDevice].systemName.floatValue >= 7.0f) {
				titeSize = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:16] } context:nil].size;
			}
			else {
//				titeSize = [title sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
				NSAttributedString *attributedText =  [[NSAttributedString alloc] initWithString:title
				                                                                      attributes:@{
				                                           NSFontAttributeName: [UIFont systemFontOfSize:16]
													   }];
				titeSize = [attributedText boundingRectWithSize:CGSizeMake(MAXFLOAT, 40)
				                                        options:NSStringDrawingUsesLineFragmentOrigin
				                                        context:nil].size;
			}
			button.frame = CGRectMake(0, 0, titeSize.width, titeSize.height);
		}

		[button setTitle:title forState:UIControlStateNormal];
		button.titleLabel.font = [UIFont systemFontOfSize:16];
		[button addTarget:self action:@selector(__handleActionForTouchUpInsideLeftButton:) forControlEvents:UIControlEventTouchUpInside];
		UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:button];
		self.navigationItem.leftBarButtonItem = leftItem;
		objc_setAssociatedObject(self, &kVCActionHandleLeftButtonKey, button, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
	}
	objc_setAssociatedObject(self, &kVCLeftButtonActionHandleTouchUpInsideBlockKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)__handleActionForTouchUpInsideLeftButton:(UIButton *)button {
	HandleBlock block = objc_getAssociatedObject(self, &kVCLeftButtonActionHandleTouchUpInsideBlockKey);
	if (block) {
		block();
	}
}

- (void)setEnableBackButton:(BOOL)enable {
	if (enable) {
		__weak __typeof(self) weakSelf = self;

		[self setNavigationLeftItem:[UIImage imageNamed:@"icon_menu"] withBlock: ^{
		    [weakSelf.navigationController popViewControllerAnimated:YES];
		}];
	}
	else {
		self.navigationItem.leftBarButtonItem = nil;
	}
}

- (void)showMenuBarButtonItem {
	__weak __typeof(self) weakSelf = self;

//	[self setNavigationLeftItem:[UIImage imageNamed:@"icon_menu"] withBlock: ^{
//	    [weakSelf didPressMenuButton];
//	}];

	[self setNavigationLeftItem:nil andTitle:@"Menu" withBlock: ^{
	    [weakSelf didPressMenuButton];
	}];
}

- (void)didPressMenuButton {
	[self.menuContainerViewController toggleLeftSideMenuCompletion:nil];
}

@end
